package com.heaven.service;

import com.heaven.mapper.ItemCatMapper;
import com.heaven.pojo.ItemCat;
import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ItemCatServiceImpl implements ItemCatService{
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        int parentId=0;
        //查询一级药品分类信息
        List<ItemCat> oneList=itemCatMapper.findItemCatList(parentId);
        //查询二级药品分类信息
        for (ItemCat oneItemCat: oneList){
            Integer oneId = oneItemCat.getId();
            List<ItemCat> twoList = itemCatMapper.findItemCatList(oneId);
            oneItemCat.setChildren(twoList);
            //查询三级药品分类信息
            for (ItemCat twoItemCat: twoList){
                Integer twoId = twoItemCat.getId();
                List<ItemCat> threeList = itemCatMapper.findItemCatList(twoId);
                twoItemCat.setChildren(threeList);
            }
        }

        return oneList;
    }

    @Override
    @Transactional
    public void updateStatus(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateStatus(itemCat);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        Date date=new Date();
        itemCat.setStatus(true).setUpdated(date).setCreated(date);
        itemCatMapper.saveItemCat(itemCat);
    }

    @Override
    @Transactional
    public void updateItemCat(ItemCat itemCat) {
        itemCat.setUpdated(new Date());
        itemCatMapper.updateItemCat(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel()==3){
            itemCatMapper.deleteItemCat3(itemCat.getId());
        }
        if (itemCat.getLevel()==2){
            itemCatMapper.deleteItemCat2(itemCat.getId());
        }
        if (itemCat.getLevel()==1){
            itemCatMapper.deleteItemCat1(itemCat.getId());
        }
    }
}
